$(function () {
    // 默认加载部门树
    vm.getDept();
});



var ztree;
var myChart;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        }
    },
    callback: {
        onClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    vm.q.curDeptId = treeNode.deptId;
    vm.curDeptName = treeNode.name;
    // 执行查询
    vm.search();
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            curDeptId:'', // 当前部门ID
            beginTime:'', // 统计开始时间
            endTime:'' // 统计终止时间
        },
        curDeptName:'',
        option : {
            title: {
                text: '部门利用率统计报表',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            color: ['#3398DB'],
            xAxis: [
                {
                    type: 'category',
                    data: [],
                    axisTick: {
                        alignWithLabel: true
                    },
                    axisLabel: {
                        interval:0,
                        rotate:-10
                    }
                }
            ],
            yAxis: [{type: 'value'}],
            series: [
                {
                    name: '使用频率',
                    type: 'bar',
                    barWidth: '60%',
                    data: []
                }
            ]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/dept/list?flag=1&deptLevel=2',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    var node = ztree.getNodeByParam("deptId", '01');
                    if (node != null) {
                        vm.curDeptName = node.name;
                        vm.q.curDeptId = node.deptId;
                        ztree.selectNode(node);
                        ztree.expandNode(node);
                    }else{
                        ztree.expandAll(true);
                    }
                    // 执行查询
                    vm.search();
                }
            });
        },
        search: function () {
            var param = {};
            if(vm.q.beginTime){
                param.beginTime = gmt2DateStr(vm.q.beginTime);
            }
            if(vm.q.endTime){
                param.endTime = gmt2DateStr(vm.q.endTime);
            }
            if(vm.q.curDeptId){
                param.curDeptId = vm.q.curDeptId;
            }
            myChart.showLoading();
            Ajax.request({
                url: "../sys/stat/dept",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    myChart.hideLoading();
                    myChart.setOption({
                        xAxis: {
                            data: res.result.xaxisData
                        },
                        series: [{
                            data: res.result.seriesData
                        }]
                    });
                }
            });
        },
        resetSearch: function () {
            vm.q.beginTime = '';
            vm.q.endTime = '';
            vm.search();
        }
    },
    mounted: function(){
        // 初始化部门树
        // 初始化echarts
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true)  //setOption 用this.option中的数据开始作图
    }
});
